package top.manbutech.ui.androidui;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;


public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private EditText mUsername;
    private EditText mPassword;
    private Button mLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qq_login_layout);
        //初始化控件
        initView();
        //初始化监听
        initListener();

    }


    //初始化监听
    private void initListener() {
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: 点击登录按钮");
                //处理登录事件
                HandlerLoginEvent();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();



    }

    //处理登录事件
    private void HandlerLoginEvent() {
        //获取账号和密码
        String username = mUsername.getText().toString();
        String password = mPassword.getText().toString();
        //对账号进行检查
//        if(username.length() == 0){
//            Toast.makeText(this,"账号不可以为空",Toast.LENGTH_SHORT).show();
//            return;
//        }
//
//        if(password.length() == 0){
//            Toast.makeText(this,"密码不可以为空",Toast.LENGTH_SHORT).show();
//            return;
//        }

        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this,"账号不可以为空",Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this,"密码不可以为空",Toast.LENGTH_SHORT).show();
            return;
        }

        //保存用户信息
        saveUserInfo(username, password);


    }

    private void saveUserInfo(String username, String password) {

        File filesDir = this.getFilesDir();
        Log.d(TAG, "getFilesDir: "+filesDir.toString());
        try{
            File file = new File("/data/data/top.manbutech.ui.androidui/info.text");
            if (file.exists()) {
                file.createNewFile();
            }
            Log.d(TAG, "saveUserInfo: 处理文件");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write((username+"***"+password).getBytes());
            fos.close();
        }catch (Exception e){

        }
    }

    //初始化控件
    private void initView() {
        mUsername = (EditText) this.findViewById(R.id.username);
        mPassword = (EditText) this.findViewById(R.id.password);
        mLogin = (Button) this.findViewById(R.id.login_btn);


    }

}
